<?php


   class ShezhiAction extends Action{
   
      public function index(){
	  
	    
	  
	     $this->sys=M('system')->where('id=1')->find();
		 $this->re=M('orderinfo')->where('id="%s"',$_GET['id'])->find();
		 
		 if($this->re['mohudu']!=''){
		   
		   $this->mohudu=$this->re['mohudu'];
		 
		 }else{
		   
		   $this->mohudu=$this->sys['mohudu']*10;
		 
		 }
	  
	     $this->id=$_GET['id'];
		 
		
	  
	     $this->display();
	  
	  }
   
   
      public function shengcheng(){
	  
	  
	             $wx=M('wx')->where('id=1')->find();
                 $APPID=$wx['appid'];
                 $APPSECRET=$wx['appsecret'];
                 $mytoken=M('token')->where('id=1')->find();


                $TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$APPSECRET;



				if(time()>$mytoken['time']){
				
				$TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$APPSECRET;
				
				$json=file_get_contents($TOKEN_URL);
				$result=json_decode($json);
				
				
				$ACC_TOKEN=$result->access_token;
				
				$st['token']=$ACC_TOKEN;
				$st['time']=time()+3600;
				M('token')->where('id=1')->save($st);
				
				}else{
				$ACC_TOKEN=$mytoken['token'];
				}
	  
	     if($_POST){
		 
		    $wangzhi=M('system')->where('id=1')->find();
		    $wangzhi=$wangzhi['url'];
		    $url=$wangzhi."/shezhimohu.php?id=".$_POST['id']."&value=".$_POST['mohudu'];
		 
		    $result=file_get_contents($url);
			
			$order=M('orderinfo')->where('id="%s"',$_POST['id'])->find();
			
			if($result){
			
			
			    $this->changesize('./images/'.$result);
				
				//dump($result);
				 //写字
				 
			    include __ROOT__.('xinshuiyin.php');
			   
			   $markimg = new ImageWatermark('./images/'.$result);

				$markimg->markPosType=7;
				
				$markimg->fontFile='wr.TTF';
				
				$markimg->color='#ffffff';
				
				$markimg->fontSize=22;
				
			    if($order['isvideo']==1){	
				 
				  $markimg->appendTextMark('长按二维码看视频',230,50);
				
				}else{
				
				 $markimg->appendTextMark('长按二维码看原图',230,50);
				}
				
				
				if($_POST['text']!=""){
				
				
				    $markimg->markPosType=2;
				
					$markimg->fontFile='wr.TTF';
					
					$markimg->color='#ffffff';
					
					$markimg->fontSize=36;
				
			   
				 
				   $markimg->appendTextMark($_POST['text']);
				
				
				}
				
				
				$markimg->write("./images/new-".$result);
			   
			    	//图片上打上二维码
				
				$erweima="./images/e-".$order['qingxi'];
				
				//dump($erweima);
				include __ROOT__.('/tupian.php');
				
				if($order['isvideo']==1){
				
			    	imageWaterMark1("./images/new-".$result, 5, "./play.png");
				
				}
			    imageWaterMark1("./images/new-".$result, 7, "./images/e-".$order['qingxi'].".png");
				
				
				
				
				
				
			   
			   	//上传素材
				
				 $url="http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=".$ACC_TOKEN."&type=image";
				$file = realpath("./images/new-".$result); //要上传的文件
				$fields['media'] = '@'.$file;
				$ch = curl_init($url) ;
				curl_setopt($ch, CURLOPT_POST, 1);
				curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
				$myimg = curl_exec($ch);
				if (curl_errno($ch)) {
				 return curl_error($ch);
				}
				curl_close($ch);				
				$myimg=json_decode($myimg,true);
				
				
				//dump( $myimg['media_id']);
				//
				//dump($order['openid']);
				
				 //客户消息回复图片
				$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$ACC_TOKEN;
                $data = '{
                    "touser": "'.$order['openid'].'", 
                    "msgtype": "image", 
                    "image": {
                       "media_id":"'.$myimg['media_id'].'"
                    }
                }';
                $this->https_request($url,$data);
			   
			   
			   
			
			   
			
			   //修改数据库orderinfo
			   
			   $arr['suijistart']=$_POST['suijistart'];
			   $arr['suijiend']=$_POST['suijiend'];
			   $arr['gudingmoney']=$_POST['gudingmoney'];
			   $arr['type']=$_POST['issuiji'];
			   $arr['mohu']=$result;
			   $arr['mohudu']=$_POST['mohudu'];
			   
			   M('orderinfo')->where('id="%s"',$_POST['id'])->save($arr);
			    
			   
			  $ajax=1;
			}else{
			
			  $ajax=2;
			}
			
			
		    $this->ajaxReturn($ajax);
		 
		 
		 }
	  
	     
	  
	  }
	  
	  
	  public function changesize($mohu){
	
	    	
			include __ROOT__."/phpthumb/ThumbLib.inc.php";
			$img_info = getimagesize($mohu);
		   
		  
            $width=$img_info['0'];
          
			
			if($width<960){
			
				if($width<100){
					  
					  $beishu=1300;
					
					}elseif($width>100 and $width<300){
					
					  $beishu=1000; 
					 
					}elseif($width>300 and $width<600){
					
					  $beishu=400;
					
					}else{
					
					  $beishu=200;
					
					}
				 
				 
				 try {   
					  $thumb = PhpThumbFactory::create($mohu);
					  $thumb->resizePercent($beishu);
					  $thumb->resize(960);
					  $thumb->save($mohu);
					} catch (Exception $e) {   
					print $e->getMessage();   
					exit();   
					}   
			}else{
			
			   $thumb = PhpThumbFactory::create($mohu);
			   $thumb->resize(960);
			   $thumb->save($mohu);
			
			}	
			   
		
	
	
	
	}
	
	
	  
	  
	  
	    public function https_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
   
   
    
   
   
   }

